package com.jhy.study2;


import com.jhy.study2.entity.Cat;
import com.jhy.study2.entity.Dog;

import java.util.*;

/**
 * @author: robert
 * @description: Map的一些练习
 * @date: 2021/8/23 14:54
 * @return
 */
public class MapTestor1 {

    public static void main(String[] args) {

        /*
        * HashMap and LinkedHashMap(基于双向列表)
        * */
//        testHashMapAndLinkedHashMap();
//        testTreeMap();
//        testTreeMap2();
//        testTreeMap3();
        testTreeMap4();



    }

    public static void testHashMapAndLinkedHashMap(){
//        Map<String,Object> map1 = new HashMap<String,Object>();
        Map<String,Object> map1 = new LinkedHashMap<>();
        map1.put("a","我是a");
        map1.put("b","我是b");
        map1.put("d","我是d");
        map1.put("c","我是c");
        map1.put("h","我是h");
        map1.put("h","我是h1");
        map1.put("h","我是h2");  //这个会覆盖前面的元素
        map1.put(null,"abc");
        map1.put("g",null);
        map1.put(null,null);

        System.out.println("size:"+map1.size());
        for(Map.Entry<String,Object> entry : map1.entrySet()){
            System.out.println("key:"+ entry.getKey() + " - value:" + entry.getValue() );
            //结论是LinkedHashMap 按插入顺序排列了，但是key一致时，后面的相同值会覆盖前面的，HashMap本身展示是无序的
        }



    }

    public  static void testTreeMap(){
        Map<String,Object> treeMap1 = new TreeMap<>();
        treeMap1.put("aa",12);
        treeMap1.put("aa",34);  //key值相同，后面的值覆盖前面的
        treeMap1.put("cc",00);
        treeMap1.put("bb","bb");
//        treeMap1.put(null,null);  //运行时提示空指针
//        treeMap1.put(null,"第二个null");   //证明treeMap的key不能为null
        treeMap1.put("hh",null);
        treeMap1.put("",null);  //空字符串也能作为key
        System.out.println("size:"+ treeMap1.size());
        for(Map.Entry<String,Object> entry : treeMap1.entrySet()){
            System.out.println(entry.getKey() + " - "+ entry.getValue());
        }



    }

    public static void testTreeMap2(){
        Map<String,Object> map1 = new TreeMap<>();
        map1.put("one",new Dog("小黑",20));
        map1.put("two",new Dog("小黑",10));
        map1.put("three",new Dog("小白",20));
        map1.put("three",new Dog("小白",10));
        for(Map.Entry<String,Object> entry : map1.entrySet()){
            System.out.println(entry.getKey() + " - "+ entry.getValue());
        }
    }


    public static void testTreeMap3(){
        Map<Dog,Object> map1 = new TreeMap<>();  //如果dog不实现compare接口就会报：java.lang.ClassCastException: com.jhy.study2.entity.Dog cannot be cast to java.lang.Comparable
        map1.put(new Dog("小黑",20),new Object());
        map1.put(new Dog("小黑",10),new Object());
        map1.put(new Dog("小白",20),new Object());
        map1.put(new Dog("小白",00),new Object());
        for(Map.Entry<Dog,Object> entry : map1.entrySet()){
            System.out.println(entry.getKey() + " - "+ entry.getValue());
        }
    }

    public static void testTreeMap4(){
        Map<Cat,Object> map1 = new TreeMap<>(new Comparator<Cat>() {
            @Override
            public int compare(Cat o1, Cat o2) {
                return o1.getCatName().compareTo(o2.getCatName());
            }
        });  //如果dog不实现compare接口就会报：java.lang.ClassCastException: com.jhy.study2.entity.Dog cannot be cast to java.lang.Comparable
        map1.put(new Cat("小黑",20),new Object());
        map1.put(new Cat("小黑",10),new Object());
        map1.put(new Cat("小白",20),new Object());
        map1.put(new Cat("小白",00),new Object());
        for(Map.Entry<Cat,Object> entry : map1.entrySet()){
            System.out.println(entry.getKey() + " - "+ entry.getValue());
        }
    }


}
